package com.lixin.smartclassroom.ui.home.tab5.activity

import android.graphics.Bitmap
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.drake.channel.sendEvent
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.addjingcaifile
import com.lixin.smartclassroom.api.uploadFiles
import com.lixin.smartclassroom.databinding.ActivityClassDetailBinding
import com.lixin.smartclassroom.ui.home.adapter.PagerAdapter
import com.lixin.smartclassroom.ui.home.tab1.fragment.ClassDetailChildFra
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.addIntArgument
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.loadRound
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible
import com.lixinkeji.utils.GlideEngine
import com.lixinkeji.utils.NativeUtil
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.config.SelectModeConfig
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.youth.banner.listener.OnPageChangeListener
import java.io.File
import kotlin.properties.Delegates

/**
 * Description 精彩时刻
 * Author: lwc
 * Date:2023/12/11 11:10
 */
class ExcitingMomentsManagerAct : BaseActivity<ActivityClassDetailBinding>() {
    override fun getViewBinding() = ActivityClassDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "精彩时刻"
    private var type = 0
    private var files = java.util.ArrayList<File>()
    private var titleName = "图片名称"

    override fun iniView() {
        binding.rlTop.gone()
        binding.btnAdd.visible()
        initSlidingLayout()
    }


    override fun onClick() {
        binding.btnAdd.onClick {
            selectPicture()
        }
    }

    private var selectMimeType by Delegates.notNull<Int>()
    private fun selectPicture() {

        selectMimeType = if (type == 0) {
            SelectMimeType.ofImage()
        } else {
            SelectMimeType.ofVideo()
        }
        PictureSelector.create(this@ExcitingMomentsManagerAct)
            .openGallery(selectMimeType)// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
            .setImageEngine(GlideEngine.createGlideEngine()).setMaxSelectNum(1)
            .setMinSelectNum(0)// 最小选择数量
            .setImageSpanCount(4)// 每行显示个数
            .setSelectionMode(SelectModeConfig.SINGLE)// 多选 or 单选PictureConfig.MULTIPLE : PictureConfig.SINGLE
            .isPreviewImage(true)// 是否可预览图片
            .isCameraAroundState(true)// 是否显示拍照按钮
            .isSelectZoomAnim(true)// 图片列表点击 缩放效果 默认true
            .forResult(object : OnResultCallbackListener<LocalMedia> {
                override fun onResult(result: java.util.ArrayList<LocalMedia>?) {
                    if (result != null) {
                        files.clear()
                        if (type == 0) {
                            for (i in 0 until result.size) {
                                val filePath: String = result[i].realPath
                                val bitmap: Bitmap =
                                    NativeUtil.compressBitmap(
                                        this@ExcitingMomentsManagerAct,
                                        filePath
                                    )
                                val file: File = NativeUtil.bitmapToFile(
                                    this@ExcitingMomentsManagerAct,
                                    bitmap,
                                    60,
                                    System.currentTimeMillis().toString()
                                )
                                files.add(file)
                            }
                        } else {
                            for (i in 0 until result.size) {
                                val filePath: String = result[i].realPath
                                val file: File = File(filePath)
                                files.add(file)
                            }
                        }

                        scopeNetLife {
                            val baseBean = uploadFiles(files)
                            if (baseBean.result == "0") {
                                titleName = if (type == 0) {
                                    "图片名称"
                                } else {
                                    "视频名称"
                                }
                                for (i in 0 until baseBean.urls?.size!!) {
                                    addjingcaifile(
                                        baseBean.urls!![i],
                                        (type + 1).toString(),
                                        titleName
                                    )
                                }
                                sendEvent("cs", "refreshList")
                            } else {
                                toast(baseBean.resultNote.toString())
                            }

                        }

                    }
                }

                override fun onCancel() {

                }
            })

    }

    private fun initSlidingLayout() {
        var titles = ArrayList<String>()
        var fragments = ArrayList<Fragment>()
        titles.add("图片")
        titles.add("视频")
        fragments.add(ClassDetailChildFra().addIntArgument("type", 8))
        fragments.add(ClassDetailChildFra().addIntArgument("type", 9))
        var mPageAdapter = PagerAdapter(supportFragmentManager, fragments, titles)

        binding.apply {
            viewPager.adapter = mPageAdapter
            tabLayout.setViewPager(viewPager)
            viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    type = position
                }

                override fun onPageScrollStateChanged(state: Int) {

                }

            })


        }
    }

}